package team526.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import team526.vo.VO;
import team526.dao.VideoDao;
import team526.entity.Video;
import team526.service.VideoService;

import java.io.*;
import java.util.List;

/**
 * #Date 2021/9/16 - 21:04
 *
 * @author 王铭 20181620310021
 */
@Api(tags = "视频处理")
@RestController
@RequestMapping("video")
@CrossOrigin(origins = {"http://localhost:8080","http://192.168.31.212:8080","http://127.0.0.1:8080","http://video.team526:8080"}, allowCredentials = "true")
public class VideoController {
    @Autowired
    VideoService videoService;

    @Value("${store-path.local-temp-dir}")
    String tempDirPath;

    @Autowired
    VideoDao videoDao;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @ApiOperation(value = "获取视频信息", notes = "")
    @ApiImplicitParam(name = "id", value = "1", required = true, paramType = "path", dataType = "String", dataTypeClass = String.class)
    @GetMapping("play/{id}")
    Video getOne(@PathVariable("id") Integer id) {
        return videoDao.getWithTags(id);
    }

    @ApiOperation(value = "查找视频，需要附加页面信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchWord", value = "搜索的限定词，可以为空", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageIndex", value = "页码起始值为1", required = true, dataTypeClass = Integer.class, example = "1"),
            @ApiImplicitParam(name = "pageCapacity", value = "每页数量，默认20", required = true, dataTypeClass = Integer.class, example = "20"),
            @ApiImplicitParam(name = "tagID", value = "视频类型id,可以为空（搜索时不指定类型）", dataTypeClass = Integer.class, example = "2"),
            @ApiImplicitParam(name = "ordered", value = "是否以播放量降序排序1为是", dataTypeClass = Integer.class, example = "1")
    })
    @GetMapping("search")
    List<Video> search(
            @RequestParam(value = "searchWord", required = false) String word,
            @RequestParam("pageIndex") Integer pageIndex,
            @RequestParam("pageCapacity") Integer pageCapacity,
            @RequestParam(value = "tagID", required = false) Integer tagID,
            @RequestParam(value = "ordered", required = false) Integer ordered) {
        return videoDao.searchVideo(word, (pageIndex - 1) * pageCapacity, pageCapacity, tagID, ordered, 1);
    }

    @GetMapping("countSearch")
    Integer countSearch(@RequestParam(value = "searchWord", required = false) String word,
                        @RequestParam("pageIndex") Integer pageIndex,
                        @RequestParam("pageCapacity") Integer pageCapacity,
                        @RequestParam(value = "tagID", required = false) Integer tagID,
                        @RequestParam(value = "ordered", required = false) Integer ordered){
        return videoDao.countSearch(word, (pageIndex - 1) * pageCapacity, pageCapacity, tagID, ordered, 1);
    }

    @ApiOperation(value = "视频信息，视频文件，视频封面，一起上传")
    @PostMapping("upload")
    VO<Video> saveTogether(@RequestParam("uuid") Integer uuid,
                           @RequestParam("desc") String desc,
                           @RequestParam("title") String title,
                           @RequestParam("main_tag_id") Integer main_tag_id,
                           @RequestParam Integer length,
                           @RequestParam(value = "sub_tag_ids", required = false) List<Integer> sub_tag_ids,
                           @RequestParam(value = "new_tag_titles", required = false) List<String> new_tag_titles,
                           @RequestPart("video_file") MultipartFile video_file,
                           @RequestPart("video_pic") MultipartFile video_pic,
                           @RequestParam("sessionKey") String sessionKey) throws IOException {
        Video video = videoService.saveVideoWithFullPart(uuid, title, main_tag_id, desc, length, sub_tag_ids, new_tag_titles, video_file, video_pic);
        if (video != null)
            return new VO<Video>(video, null, "upload success");
        return new VO<>(null, null, "upload failed");
    }


//    @GetMapping("videosOfUser/{uuid}")
//    public List<Video> videosOfUser(@PathVariable Integer uuid) {
//
//        return videoDao.getVideosByUuid(uuid, page, num);
//    }
}